package com.aode.music.adapter.me;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.aode.common.bean.response.Music;
import com.aode.music.R;
import com.aode.music.databinding.ItemDownloadBinding;

import java.util.ArrayList;

public class DownLoadAdapter  extends RecyclerView.Adapter{
    private Context context;
    private ArrayList<Music> musicArrayList;

    public DownLoadAdapter (Context context,ArrayList<Music> musicArrayList){
        this.context = context;
        this.musicArrayList = musicArrayList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemDownloadBinding downloadBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_download,parent,false);
        return new DownLoadHolder(downloadBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof DownLoadHolder){
            DownLoadHolder downloadHolder = (DownLoadHolder) holder;
            if(musicArrayList!=null&&!musicArrayList.isEmpty()&&musicArrayList.size()>0){
                Music music = musicArrayList.get(position);
                String url = music.getMusicCover();
                if(url!=null&&!url.equals("")){
                    Bitmap bitmap = BitmapFactory.decodeFile(url);
                    downloadHolder.getDownloadBinding().ivMusic.setImageBitmap(bitmap);
                }

               downloadHolder.getDownloadBinding().setMusic(music);
            }
        }
    }

    @Override
    public int getItemCount() {
        return musicArrayList.size();
    }


    class DownLoadHolder extends RecyclerView.ViewHolder{
        private ItemDownloadBinding downloadBinding;
        public DownLoadHolder(ItemDownloadBinding downloadBinding) {
            super(downloadBinding.getRoot());
            this.downloadBinding =downloadBinding;
        }

        public ItemDownloadBinding getDownloadBinding() {
            return downloadBinding;
        }

        public void setDownloadBinding(ItemDownloadBinding downloadBinding) {
            this.downloadBinding = downloadBinding;
        }
    }

}
